﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WindowsLive;

namespace goMatic
{
    public partial class XMLRPCInit : System.Web.UI.Page
    {
        const string LoginCookie = "webauthtoken";

        // Initialize the WindowsLiveLogin module.
        static WindowsLiveLogin wll = new WindowsLiveLogin(true);

        protected static string AppId = wll.AppId;
        protected string UserId;
        protected Customers oCostomer;
        protected dbEntities oDB;
        protected string CustomerId;
        protected string sSessionID;
        protected void Page_Load(object sender, EventArgs e)
        {
            /* If the user token has been cached in a site cookie, attempt
               to process it and extract the user ID. */
            HttpRequest req = HttpContext.Current.Request;
            HttpCookie loginCookie = req.Cookies[LoginCookie];

            if (!Page.IsPostBack)
            {
                if (loginCookie != null)
                {
                    string token = loginCookie.Value;

                    if (!string.IsNullOrEmpty(token))
                    {
                        WindowsLiveLogin.User user = wll.ProcessToken(token);

                        if (user != null)
                        {
                            UserId = user.Id;
                            dbEntities oDB = new dbEntities();
                            CustomerId = oDB.Customers.First(t => t.liveid == UserId).customerid;
                            ViewState.Add("CustomerID", CustomerId);
                        }
                    }
                    else
                    {
                        Response.Redirect("~/Default.aspx");
                    }
                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
            else
            {
                CustomerId = ViewState["CustomerID"].ToString();
                if (string.IsNullOrEmpty(CustomerId))
                {
                    Response.Redirect("~/Default.aspx");
                }
            }


        }


        protected void btInitStart_Click(object sender, EventArgs e)
        {
            oDB = new dbEntities();
            //oCostomer = oDB.Customers.First(t => t.customerid == UserId);


            if (!string.IsNullOrEmpty(CustomerId))
            {

                CCU.ccucmdSoapClient oCCU = new CCU.ccucmdSoapClient();
                oCCU.Open();
                try
                {
                    bool bres = oCCU.InitCGIStart(CustomerId);
                    if (bres)
                    {
                        lStartDone.Text = "Done";
                        lStartError.Text = "";
                    }
                    else
                    {
                        lStartDone.Text = "";
                        lStartError.Text = "an Error occured...";
                    }
                }
                catch (Exception ex)
                {
                    lStartDone.Text = "";
                    lStartError.Text = "Error: " + ex.Message; ;
                }
                /*
                                if (string.IsNullOrEmpty(sSessionID))
                                {
                                    sSessionID = oCCU.GetSessionID_CGI(oCostomer.external_ip, int.Parse(oCostomer.cgiport), oCostomer.username, oCostomer.password);
                                    //btInitStart.Text = sSessionID;
                                }
                                if (!string.IsNullOrEmpty(sSessionID))
                                {
                                    bool b1 = oCCU.InitCGI("BidCos-RF", "http://xmlrpc.azurewebsites.net:80/xmlrpc.aspx?id=" + oCostomer.customerid + "&port=2001", oCostomer.customerid, sSessionID, oCostomer.external_ip, int.Parse(oCostomer.cgiport), "", "");
                                    bool b2 = oCCU.InitCGI("BidCos-Wired", "http://xmlrpc.azurewebsites.net:80/xmlrpc.aspx?id=" + oCostomer.customerid + "&port=2000", oCostomer.customerid, sSessionID, oCostomer.external_ip, int.Parse(oCostomer.cgiport), "", "");
                                    bool b3 = oCCU.InitCGI("System", "http://xmlrpc.azurewebsites.net:80/xmlrpc.aspx?id=" + oCostomer.customerid + "&port=2002", oCostomer.customerid, sSessionID, oCostomer.external_ip, int.Parse(oCostomer.cgiport), "", "");
                                    if (b1 & b2 & b3)
                                    {
                                        lStartDone.Text = "Done";
                                        lStartError.Text = "";
                                    }

                                    else
                                    {
                                        lStartDone.Text = "";
                                        lStartError.Text = "RF:" + b1.ToString() + " WR:" + b2.ToString() + " SY:" + b3.ToString();
                                    }
                                    return;
                                }
                                else
                                {
                                    lStartError.Text = "No SessionID from CCU !";
                                }
                */
                oCCU.Close();

            }

        }

        protected void btInitStop_Click(object sender, EventArgs e)
        {
            oDB = new dbEntities();
            //oCostomer = oDB.Customers.First(t => t.customerid == UserId);
            //CustomerID = oCostomer.customerid;

            if (!string.IsNullOrEmpty(CustomerId))
            {

                CCU.ccucmdSoapClient oCCU = new CCU.ccucmdSoapClient();
                oCCU.Open();
                try
                {
                    bool bres = oCCU.InitCGIStop(CustomerId);
                    if (bres)
                    {
                        lStopDone.Text = "Done";
                        lStopError.Text = "";
                    }
                    else
                    {
                        lStopDone.Text = "";
                        lStopError.Text = "an Error occured...";
                    }
                }
                catch (Exception ex)
                {
                    lStopDone.Text = "";
                    lStopError.Text = "Error: " + ex.Message; ;
                }
                oCCU.Close();

            }
        }

        protected void btImportDevices_Click(object sender, EventArgs e)
        {
            try
            {
                oDB = new dbEntities();

                CCU.ccucmdSoapClient oCCU = new CCU.ccucmdSoapClient();
                oCCU.Endpoint.Binding.CloseTimeout = new TimeSpan(0, 3, 0);
                oCCU.Endpoint.Binding.SendTimeout = new TimeSpan(0, 2, 0);
                oCCU.Open();
                if (oCCU.ImportDevices(CustomerId))
                {
                    lImpDone1.Text = "Done";
                    lImpError.Text = "";
                }
                else
                {
                    lImpDone1.Text = "";
                    lImpError.Text = "an Error occured...";
                }
                oCCU.Close();
            }
            catch (Exception ex)
            {
                lImpDone1.Text = "";
                lImpError.Text = ex.Message;
            }
        }

        protected void btImportRooms_Click(object sender, EventArgs e)
        {
            oDB = new dbEntities();

            CCU.ccucmdSoapClient oCCU = new CCU.ccucmdSoapClient();
            oCCU.Endpoint.Binding.CloseTimeout = new TimeSpan(0, 3, 0);
            oCCU.Endpoint.Binding.SendTimeout = new TimeSpan(0, 2, 0);
            oCCU.Open();
            if (oCCU.ImportRooms(CustomerId))
            {
                lRoomsDone.Text = "Done";
                lRoomsError.Text = "";
            }
            else
            {
                lRoomsDone.Text = "";
                lRoomsError.Text = "an Error occured...";
            }
            oCCU.Close();
        }

        protected void btImportFunctions_Click(object sender, EventArgs e)
        {
            oDB = new dbEntities();

            CCU.ccucmdSoapClient oCCU = new CCU.ccucmdSoapClient();
            oCCU.Endpoint.Binding.CloseTimeout = new TimeSpan(0, 3, 0);
            oCCU.Endpoint.Binding.SendTimeout = new TimeSpan(0, 2, 0);
            oCCU.Open();
            if (oCCU.ImportFunctions(CustomerId))
            {
                lFunctionsDone.Text = "Done";
                lFunctionsError.Text = "";
            }
            else
            {
                lFunctionsDone.Text = "";
                lFunctionsError.Text = "an Error occured...";
            }
            oCCU.Close();
        }
    }
}